package com.huifai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huifai.entity.SysPermission;
import com.huifai.entity.SysRolePermission;
import com.huifai.mapper.SysPermissionMapper;
import com.huifai.mapper.SysRolePermissionMapper;
import com.huifai.service.ISysPermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.rmi.runtime.Log;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 菜单权限表 服务实现类
 * </p>
 *
 * @author wcj
 * @since 2019-07-16
 */
@Service
@Slf4j
public class SysPermissionServiceImpl extends ServiceImpl<SysPermissionMapper, SysPermission> implements ISysPermissionService {
    @Autowired
    private SysRolePermissionMapper sysRolePermissionMapper;
    @Autowired
    private SysPermissionMapper sysPermissionMapper;

    @Override
    public List<SysPermission> findSysPermissionsByRoles(List<Long> roleIds) {
        if(roleIds.size()!=0){
            QueryWrapper<SysRolePermission> queryWrapper=new QueryWrapper<>();
            queryWrapper.in("role_id",roleIds);
            List<SysRolePermission> sysRolePermissionList=sysRolePermissionMapper.selectList(queryWrapper);
            if(sysRolePermissionList!=null&&sysRolePermissionList.size()>0){
                List<Long> permissionIds=new ArrayList<>();
                for (SysRolePermission sysRolePermission : sysRolePermissionList) {
                    permissionIds.add(sysRolePermission.getPermissionId());
                }
                QueryWrapper<SysPermission> permissionQuery=new QueryWrapper<>();
                permissionQuery.in("id",permissionIds);
                return sysPermissionMapper.selectList(permissionQuery);
            }
        }
        return null;
    }


    @Override
    public List<SysPermission> findSysPermissions(List<Long> roleIds,String code) {
        List<SysPermission> sysPermissList = new ArrayList<>();
        List<SysPermission> sysPermission = new ArrayList<>();
        if(roleIds.size()!=0){
            QueryWrapper<SysRolePermission> queryWrapper=new QueryWrapper<>();
            queryWrapper.in("role_id",roleIds);
            List<SysRolePermission> sysRolePermissionList=sysRolePermissionMapper.selectList(queryWrapper);  //通过角色ID去查角色权限关联表
            if(sysRolePermissionList!=null&&sysRolePermissionList.size()>0){
                List<Long> permissionIds=new ArrayList<>();
                for (SysRolePermission sysRolePermission : sysRolePermissionList) {
                    log.info("日志信息"+sysRolePermission.getPermissionId());
                    permissionIds.add(sysRolePermission.getPermissionId());
                }
                QueryWrapper<SysPermission> permissionQuery=new QueryWrapper<>();
                permissionQuery.in("id",permissionIds);
                log.info("------------------------"+permissionIds);
            //    permissionQuery.eq("app_code",code);
                List<SysPermission> sysPermissionList = sysPermissionMapper.selectList(permissionQuery);
                log.info("sysPermissionList长度为："+sysPermissionList.size());
                for (SysPermission sys : sysPermissionList){
                    String [] arr = sys.getAppCode().split(",");
                    for (String i : arr){
                        if (i.equals(code)){
                            sysPermissList.add(sys);
                        }
                    }
                }
                return sysPermissList;
            }
        }
        return null;
    }

    @Override
    public List<SysPermission> findSysPermissionsByAll(String code) {
        List<SysPermission> sysPermissionList = new ArrayList<>();
        QueryWrapper<SysPermission> permissionQuery=new QueryWrapper<>();
        if (StringUtils.isNotBlank(code)){
            //查出来菜单表所有数据
            //便利数据取出 code
            //切割code数组
            List<SysPermission> sysPermissions = sysPermissionMapper.selectList(permissionQuery);
            for (SysPermission sys : sysPermissions){
                String appCode = sys.getAppCode();
                String [] arr = appCode.split(",");
                for (String i : arr){
                    if (i.equals(code)){
                        sysPermissionList.add(sys);
                    }
                }
            }
            //sysPermissionMapper.selectList(permissionQuery);
            //permissionQuery.eq("app_code",code);
        }
        return sysPermissionList;
    }
}
